package com.bhs.sansonglogistics.ui.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bhs.sansonglogistics.BuildConfig;
import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.AppManager;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.event.CloseConnect;
import com.bhs.sansonglogistics.bean.event.Event;
import com.bhs.sansonglogistics.ui.login.PhoneNumberLoginActivity;
import com.bhs.sansonglogistics.ui.login.SplashActivity;
import com.bhs.sansonglogistics.utils.CacheDataManager;
import com.bhs.sansonglogistics.utils.OneClickLoginUtils;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * 设置
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener {


    private OneClickLoginUtils oneClickLoginUtils;

    private TextView mTvCacheSize;

    @Override
    public int initLayout() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("设置");
        findViewById(R.id.btn_log_out).setOnClickListener(this);
        findViewById(R.id.ll_order_inform).setOnClickListener(this);
        findViewById(R.id.ll_personalInformation).setOnClickListener(this);
        findViewById(R.id.ll_account_security).setOnClickListener(this);
        findViewById(R.id.ll_message_setting).setOnClickListener(this);
        findViewById(R.id.ll_about_Us).setOnClickListener(this);
        TextView mTvVersion = findViewById(R.id.tv_version);
        mTvVersion.setText(String.format("V%s", BuildConfig.VERSION_NAME));
        findViewById(R.id.ll_cache).setOnClickListener(this);
        mTvCacheSize = findViewById(R.id.tv_cache_size);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initData() {
        oneClickLoginUtils = new OneClickLoginUtils(this);
        try {
            mTvCacheSize.setText(CacheDataManager.getTotalCacheSize(this) + "M");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_log_out:
                EventBus.getDefault().post(new CloseConnect());
                SharedPreferencesUtils.clear(mContext, "token");
                SharedPreferencesUtils.clear(mContext, "SuccessfulAuthentication");
                SharedPreferencesUtils.clear(mContext, "isSpecialLine");
                AppManager.getInstance().finishAllActivity();
                loadingPopupShow();
                oneClickLoginUtils.pullPage();
                break;
            case R.id.ll_order_inform:
                startActivity(new Intent(mActivity, OrderInformActivity.class));
                break;
            case R.id.ll_personalInformation:
                startActivity(new Intent(mActivity, PersonalInformationActivity.class));
                break;
            case R.id.ll_account_security:
                startActivity(new Intent(mActivity, AccountSecurityActivity.class));
                break;
            case R.id.ll_message_setting:
                startActivity(new Intent(mActivity, MessageSetActivity.class));
                break;
            case R.id.ll_about_Us:
                startActivity(new Intent(mActivity, AboutUsActivity.class));
                break;
            case R.id.ll_cache:
                loadingPopupShow();
                CacheDataManager.clearAllCache(this);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadingPopupDismiss();
                        //要延时的程序
                        ToastUtil.show("清除成功");
                        mTvCacheSize.setText("0.0M");
                    }
                }, 1000);
                break;
        }
    }
}